package com.crrc.yzt.demo.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseExpandableListAdapter
import com.crrc.yzt.demo.blue.databinding.ItemChildBinding
import com.crrc.yzt.demo.blue.databinding.ItemGroupBinding
import com.crrc.yzt.demo.model.CarDetail
import com.crrc.yzt.demo.model.ItemCar
import com.crrc.yzt.demo.model.ItemCarGroup

class CarDetailAdapter(private val context: Context) : BaseExpandableListAdapter() {

    var carDetail : CarDetail? = null

    override fun getGroupCount(): Int {
        return carDetail?.listGroup?.size?:0
    }

    override fun getChildrenCount(groupPosition: Int): Int {
        return getGroup(groupPosition)?.childList?.size?:0
    }

    override fun getGroup(groupPosition: Int): ItemCarGroup? {
        return carDetail?.listGroup?.get(groupPosition)
    }

    override fun getChild(groupPosition: Int, childPosition: Int): ItemCar? {
        var itemCarGroup = getGroup(groupPosition)
        return itemCarGroup?.childList?.get(childPosition)
    }

    override fun getGroupId(groupPosition: Int): Long {
        return 0
    }

    override fun getChildId(groupPosition: Int, childPosition: Int): Long {
        return 0
    }

    override fun hasStableIds(): Boolean {
        return false
    }

    override fun getGroupView(
        groupPosition: Int,
        isExpanded: Boolean,
        convertView: View?,
        parent: ViewGroup?
    ): View {

        val itemGroup = ItemGroupBinding.inflate(LayoutInflater.from(context),parent,false)

        val group = getGroup(groupPosition)

        if (group != null) {
            itemGroup.tvGroup.text = group.name
        }

        return itemGroup.root
    }

    override fun getChildView(
        groupPosition: Int,
        childPosition: Int,
        isLastChild: Boolean,
        convertView: View?,
        parent: ViewGroup?
    ): View {

        val itemChild = ItemChildBinding.inflate(LayoutInflater.from(context),parent,false)

        val child = getChild(groupPosition,childPosition)

        if (child != null) {
            itemChild.tvChild.text = child.item
        }

        return itemChild.root
    }

    override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean {
        return false
    }

}